#include "database.h"

#include <QStringList>
#include <QSettings>
#include <QDir>
#include <QDebug>

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include "dbtabbase.h"
//#include "dbtabrecord.h"
#include "databasequery.h"
#include "dbtabproduct.h"
#include "dbtabrecordhlo.h"
#include "dbtabrecordclass.h"
#include "dbtabtabversion.h"
#include "dbtabuser.h"
#include "common/common.h"
#include "dbtabclassifierproduct.h"
//#include "dbtabformulainfo.h"
//#include "dbtabidsetting.h"
//#include "formulaweightrecord.h"

#include <QSqlDriver>
#define DATABASE_20210109 20210109
#define DATABASE_VERSION  DATABASE_20210109

const QLatin1String DataBase::DB_VERSION_NAME = QLatin1String("db_version");
const QLatin1String DataBase::DATABASE_NAME = QLatin1String("CheckWeighter.db");


DataBase::DataBase()
{



#if QT_VERSION < 0X050000
    QDir dir = QDir(USR_DIR);
    if(!dir.exists())
        DoSystem("mkdir "USR_DIR" -p");
#else
    QDir dir = QDir("./");
#endif

#ifdef MYSQL
    mDb = QSqlDatabase::addDatabase("QMYSQL");
    //mDb.setDatabaseName(DATABASE_NAME);
    mDb.setPort(3306);
#else
    mDb = QSqlDatabase::addDatabase("QSQLITE");
    mDb.setDatabaseName(USR_DIR+DATABASE_NAME);
#endif
    if(!mDb.open()){
        qDebug()<<"#$$#$#$#$#$$############################$$$$$$$$"<<mDb.lastError();
        exit(-1);
    }else{//#ifdef MYSQL
#ifdef MYSQL
        DataBaseQuery query(mDb);
        if(query.exec("CREATE DATABASE IF NOT EXISTS "DATABASE_NAME))
        {
            mDb.close();
            mDb.setDatabaseName(DATABASE_NAME);
            if(!mDb.open()){
                exit(-1);
            }
        }
        else
        {
             qDebug()<<"exec "<<mDb.lastError();
        }
#endif
    }
    bool newdb = false;
    /*
    DataBaseQuery query(mDb);
    query.deleteTable(DBTabRecord::TAB_NAME);
    query.deleteTable(DBTabRecord::TAB_NAME_CURR);
    */
    QStringList tabs = mDb.tables();qDebug()<<tabs;
//    mTabMap.insert(DBTabDataBaseInfo::TAB_NAME,DBTabDataBaseInfo::getDBTab());
//    mTabMap.insert(DBTabProduct::TAB_NAME,DBTabProduct::getDBTab());
    mTabMap.insert(DBTabProduct::TAB_NAME,DBTabProduct::getDBTab());
    mTabMap.insert(DBTabClassifierProduct::TAB_NAME,DBTabClassifierProduct::getDBTab());
    mTabMap.insert(DBTabRecordHLO::TAB_NAME,DBTabRecordHLO::getDBTab());
    mTabMap.insert(DBTabRecordClass::TAB_NAME,DBTabRecordClass::getDBTab());
    //mTabMap.insert(DBTabRecord::TAB_NAME,DBTabRecord::getDBTab());
    //mTabMap.insert(DBTabTabVersion::TAB_NAME,DBTabTabVersion::getDBTab());
    mTabMap.insert(DBTabUser::TAB_NAME,DBTabUser::getDBTab());
    //mTabMap.insert(DBTabFormulaInfo::TAB_NAME,DBTabFormulaInfo::getDBTab());
    //mTabMap.insert(FormulaWeightRecord::TAB_NAME,FormulaWeightRecord::getDBTab());
//    if(!tabs.contains(DBTabIdSetting::TAB_NAME))
//        DBTabIdSetting::createTab(mDb);
    //DBTabRecord::getDBTab()->createTab(mDb);
    for(auto iter=mTabMap.begin();iter!=mTabMap.end();iter++){
        if(tabs.contains(iter.key())==false){
            iter.value()->createTab(mDb);
            newdb  = true;
        }
    }
//    if(newdb){
//        setDataBaseVersion(DATABASE_VERSION);
//    }
//    UpdateDataBaseVersion();
}


int DataBase::getDataBaseVersion()
{
    return DBTabTabVersion::getVersion(DB_VERSION_NAME,mDb);
}

void DataBase::setDataBaseVersion(int i)
{
    DBTabTabVersion::setVersion(DB_VERSION_NAME,i,mDb);
}

void DataBase::UpdateDataBaseVersion()
{
    int oldVersion = getDataBaseVersion();
    if(oldVersion>DATABASE_VERSION)    //数据库版本太高，程序版本太老
    {
        //MyMessageDialog::warning(0,tr("Warnning"),tr("you software version to low,please update"));
    }
    else if(oldVersion<DATABASE_VERSION)
    {
        setDataBaseVersion(DATABASE_VERSION);
    }

}



bool DataBase::execRenameTab(const QString &oldname, const QString &newName)
{
    QString str="ALTER TABLE "+oldname + " RENAME TO \'" +newName +"\'";
    DataBaseQuery query(mDb);
    return query.exec(str);
}


QSqlDatabase& DataBase::getDataBase()
{
    return getObj()->mDb;
}


